import Mock from 'mockjs'
const { datalist } = Mock.mock({
  'datalist|1440': [
    {
      timesTamp: '@date(yyyy-MM-dd HH:mm:00)',
      totalField: '@float(49180.98,49190.50,94,50)',
      magneticAngle: '@float(1.391,1.401,3,4)',
      magneticInclination: '@float(42.50,43.90,94,50)',
      northboundComponent: '@float(36238.98,36240.50,94,50)',
      verticalComponents: '@float(36238.98,36240.50,94,50)',
      sastorientedComponent: '@float(-890.98,-888.50,94,50)'
    }
  ]
})

function randomData() {
  const { datalist } = Mock.mock({
    datalist: [
      {
        timesTamp: '@date(yyyy-MM-dd HH:mm:00)',
        totalField: '@float(49180.98,49190.50,94,50)',
        magneticAngle: '@float(1.391,1.401,3,4)',
        magneticInclination: '@float(42.50,43.90,94,50)',
        northboundComponent: '@float(36238.98,36240.50,94,50)',
        verticalComponents: '@float(36238.98,36240.50,94,50)',
        sastorientedComponent: '@float(-890.98,-888.50,94,50)'
      }
    ]
  })
  return datalist[0]
}

export default {
  findDatabytop: (config) => {
    // 起初，config.body的形式为：top:60
    // 为了获取 60 ,采用解构赋值，直接将top的值取出来
    const { top } = JSON.parse(config.body)
    // console.log(top)
    let newdatalist = datalist.slice(0, top)
    console.log(newdatalist)

    // shift：删除数组的第一项，数组长度减1，并返回新的数组
    datalist.shift()
    // push：将新数据推入数组末尾
    datalist.push(randomData())

    return {
      code: 200,
      message: '获取数据列表成功',
      data: newdatalist
    }
  }
}
